<?php

namespace Admin\Controller;

use Think\Controller;

class CommonController extends Controller {

    protected $auth = TRUE;
    public $avoidAuth = array();

    public function _initialize() {
        $this->checkAuth($this->avoidAuth);
    }

    public function setAvoidAuth($param) {
        $this->avoidAuth = $param;
    }

    //检查是否验证
    public function checkAuth($array = array()) {
        if (in_array(ACTION_NAME, $array))
            $this->setAuth();
        $this->checkUser();
    }

    //生成系统时间
    protected function crtTime() {
        return date('Y-m-d H:i:s');
    }

    public function __construct() {
        parent::__construct();
    }

    function checkUser() {
        if ($this->auth)
            $this->checkUserAuth();
        $this->setAuth(TRUE);
    }

    public function setAuth($status = FALSE) {
        $this->auth = $status;
    }

    //验证用户是否登录
    public function checkUserAuth() {
        if (empty($_SESSION['USER'])) {
            echo '<script>window.location.href="' . __APP__ . '/Index/login"</script>';
        }
    }

    function DeleteHtml($str) {
        $str = trim($str);
        $str = strip_tags($str, "");
        $str = ereg_replace("\t", "", $str);
        $str = ereg_replace("\r\n", "", $str);
        $str = ereg_replace("\r", "", $str);
        $str = ereg_replace("\n", "", $str);
        $str = ereg_replace(" ", " ", $str);
        return trim($str);
    }

    function Export($data, $node) {
        $str = $this->OConversionJson($data, $node);
        if (!empty($str)) {
            $rs = $str;
        } else {
            $rs = 0;
        }
        return $this->DeleteHtml($rs);
    }

    function OConversionJson($data, $node) {
        //$data = $this->DeleteHtml($data);
        $len = count($data);
        if (!$data) {
            return FALSE;
        }
        $json = '';
        if ($len < 1) {
            return FALSE;
        }
        if ($len > 2) {
            if (array_keys($data[0]) != array_keys($data[1])) {
                return FALSE;
            }
        }

        //键名
        $keyName = array_keys($data[0]);
        $keyLen = count($keyName);
        //循环开始
        for ($i = 0; $i < $len; $i++) {
            $json = $json . '{';
            for ($j = 0; $j < $keyLen; $j++) {
                if ('id' == $keyName[$j]) {
                    $keyId = 'sid';
                } else {
                    $keyId = $keyName[$j];
                }
                $str = str_replace('"', '\"', $data[$i][$keyName[$j]]);
                $str = str_replace("'", "\'", $str);
                $json = $json . '"' . $keyId . '":"' . $str . '",';
            }
            //删除字符串最后一个字符
            $json = substr($json, 0, (strlen($json) - 1));
            $json = $json . '},';
        }
        //删除字符串最后一个字符
        $json = substr($json, 0, (strlen($json) - 1));
        $data = '[' . $json . ']';
        return '{' . $node . ':' . $data . '}';
    }

    //上传文件
    public function upload() {
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = './uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件 
        $info = $upload->upload();
        
        if (!$info) {// 上传错误提示错误信息
            //$this->error($upload->getError());
            return false;
        } else {// 上传成功
            return $upload->rootPath . $info['imgUrl']['savepath'] . $info['imgUrl']['savename'];
        }
    }

}

?>